<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\common\Attribute;
use think\facade\View;
use app\model\AdminRole;
use app\model\Admin;

#[Attribute(title: "角色管理")]
class AdminRoleController extends BaseController
{
    #[Attribute(isAuth: true, isPage: true, title: "查询")]
    public function query()
    {
        if (request()->isPost()) {
            $all = request()->param();
            $data = AdminRole::where(function ($data) use ($all) {
                if (isset($all["pid"])) {
                    $data->where("pid", $all["pid"]);
                } else {
                    $data->where("pid", 0)->page((int)$all["page"], (int)$all["limit"]);
                }
            })->select()->each(function ($v, $k) {
                $find = AdminRole::where("pid", $v["id"])->find();
                if ($find) {
                    $v["isParent"] = true;
                } else {
                    $v["isParent"] = false;
                }
            });
            return json([
                "code" => 0,
                "msg" => "获取成功",
                "count" => count($data),
                "data" => $data->toArray(),
            ]);
        }
        return View::fetch();
    }

    #[Attribute(isAuth: true, isPage: true, title: "添加")]
    public function add()
    {
        $parent_data = AdminRole::select();
        View::assign(name: ["parent_data" => selectTree($parent_data->toArray())]); //顶级选项
        $appAttribute = appAttribute(false, "children"); //权限组
        View::assign(name: ["appAttribute" => json_encode(array_values($appAttribute), 320)]);
        if (request()->isPost()) {
            $all = request()->param();
            try {
                validate(\app\admin\validate\AdminRoleValidate::class)
                    ->scene("add")
                    ->check($all);
                $all["auth"] =  json_encode(explode(",", $all["auth"]), 320);
                $res = AdminRole::create($all);
                if ($res) {
                    return success("操作成功");
                } else {
                    return error("操作失败");
                }
            } catch (\think\exception\ValidateException $e) {
                return error(msg: "验证失败", data: $e->getError());
            }
        }
        return View::fetch();
    }

    #[Attribute(isAuth: true, isPage: false, title: "编辑")]
    public function edit()
    {
        if (request()->isGet()) {
            $id = request()->get('id');
            $find = AdminRole::find($id);
            View::assign("find", $find);
            $parent_data = AdminRole::where("id", "not in", $id)->select();
            View::assign(name: ["parent_data" => selectTree($parent_data->toArray())]); //顶级选项
            $appAttribute = appAttribute(false, "children"); //权限组
            View::assign(name: ["appAttribute" => json_encode(array_values($appAttribute), 320)]);
        }
        if (request()->isPost()) {
            $all = request()->param();
            try {
                validate(\app\admin\validate\AdminRoleValidate::class)
                    ->scene("edit")
                    ->check($all);
                $all["auth"] =  json_encode(explode(",", $all["auth"]), 320);
                $res = AdminRole::update($all);
                if ($res) {
                    return success("操作成功");
                } else {
                    return error("操作失败");
                }
            } catch (\think\exception\ValidateException $e) {
                return error(msg: "验证失败", data: $e->getError());
            }
        }

        return View::fetch();
    }

    #[Attribute(isAuth: true, isPage: false, title: "删除")]
    public function del()
    {
        if (request()->isDelete()) {
            $all = request()->param();
            $find = Admin::where("rid", $all["id"])->find();
            if ($find) {
                return error("该角色已在使用中");
            }
            $child = AdminRole::where("pid", $all["id"])->find();
            if ($child) {
                return error("请先删除下级");
            }
            $res = AdminRole::destroy($all["id"]);
            if ($res) {
                return success("操作成功");
            } else {
                return error("操作失败");
            }
        }
    }
}
